package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketBasicInfo;
import com.cskaoyan.market.service.DashboardService;
import com.cskaoyan.market.service.impl.DashboardServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/admin/dashboard")
public class AdminDashBoardController extends HttpServlet {

    DashboardService dashboardService = new DashboardServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/", "");

        if("dashboard".equals(op)){
            dashboard(req, resp);
        }
    }

    private void dashboard(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        MarketBasicInfo dashboard = dashboardService.dashboard(req, resp);

        Object ok = ResponseUtil.ok(dashboard);
        String s = JacksonUtil.getObjectMapper().writeValueAsString(ok);
        resp.getWriter().write(s);


    }


}
